<template>
  <div>
    <el-dialog
      :title="info.isadd ? '规格添加' : '规格修改'"
      :visible.sync="info.isshow"
      width="50%"
      @closed="closed"
    >
      <div>form:{{ form }}</div>
      <div>arr:{{arr}}</div>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="规格名称">
          <el-input v-model="form.specsname"></el-input>
        </el-form-item>
        <el-form-item label="规格属性" v-for="(item,index) in arr"
        :key="index">
          <div class="line">
            <el-input v-model="item.val"></el-input>
            <el-button type="primary" v-if="index==0" @click="arr.push({val:''})">新增规格属性</el-button>
            <el-button type="danger" @click="arr.splice(index,1)" v-else>删除</el-button>
          </div>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-if="info.isadd" @click="Add">添加</el-button>
          <el-button type="primary" v-else @click="Edit">修改</el-button>
          <el-button type="info"  @click="qx">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqspecsadd,reqspecsinfo,reqspecsedit } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  data() {
    return {
      arr: [{ val: "" }, { val: "" }],
      form: {
        specsname: "",
        attrs: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
        list:"specs/list"
    }),
  },
  mounted() {
      this.reqList();
  },
  methods: {
    ...mapActions({
        reqList:"specs/reqList"
    }),
    Add(){
      this.form.attrs=this.arr.map((item)=>item.val).join(",");
      reqspecsadd(this.form).then((res)=>{
          if(res.data.code == 200){
              successAlert(res.data.msg);
              this.info.isshow=false;
              this.reqList();
          }
      })
  },
  getone(id){
      reqspecsinfo({id:id}).then((res)=>{
        console.log(id);
          if(res.data.code == 200){
              successAlert(res.data.msg);
              this.form = res.data.list[0];
              this.arr = this.form.attrs.map((item) => ({ val: item }));
              this.form.id=id;
          }
      })
  },
  null1(){
    this.form={
      specsname: "",
        attrs: "",
        status: 1,
    }
    this.arr=[{ val: "" }, { val: "" }]
  },
  //修改
  Edit(){
    this.form.attrs=this.arr.map((item)=>item.val).join(",");
    reqspecsedit(this.form).then((res)=>{
      if(res.data.code == 200){
              successAlert(res.data.msg);
              this.info.isshow=false;
              this.null1();
              this.reqList();
          }
    })
  },
  qx(){
    if(!this.info.isadd){
      this.null1();
    }
    this.info.isshow=false;
  },
  closed(){
    if(!this.info.isadd){
      this.null1();
    }
    this.info.isshow=false;
    
  }
  },
  
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.line {
  display: flex;
  .el-input {
    flex: 1;
  }
}
</style>